package com.ruoyi.web.controller.exhibition;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.domain.AcProjectConfig;
import com.ruoyi.demo.service.IAcProjectConfigService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.log4j.Log4j2;

/**
 * 工程配置Controller
 * 
 * @author CMee
 * @date 2025-10-24
 */
@Log4j2
@RestController
@RequestMapping("/demo/project_config")
public class AcProjectConfigController extends BaseController {
    @Autowired
    private IAcProjectConfigService acProjectConfigService;

    /**
     * 查询工程配置列表
     */
    @PreAuthorize("@ss.hasPermi('demo:project_config:list')")
    @GetMapping("/list")
    public TableDataInfo list(AcProjectConfig acProjectConfig) {
        LambdaQueryWrapper<AcProjectConfig> lqw = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(acProjectConfig.getProjectName())) {
            lqw.like(AcProjectConfig::getProjectName, acProjectConfig.getProjectName());
        }
        // lqw.eq(AcProjectConfig::getSysUserId, acProjectConfig.getSysUserId());
        lqw.orderByDesc(AcProjectConfig::getTableId);
        Page<AcProjectConfig> page =
            acProjectConfigService.page(Page.of(acProjectConfig.getPageNum(), acProjectConfig.getPageSize()), lqw);
        log.info("page = {}", page);
        return getDataTable(page);
    }

    /**
     * 导出工程配置列表
     */
    @PreAuthorize("@ss.hasPermi('demo:project_config:export')")
    @Log(title = "工程配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcProjectConfig acProjectConfig) {
        List<AcProjectConfig> list = acProjectConfigService.selectAcProjectConfigList(acProjectConfig);
        ExcelUtil<AcProjectConfig> util = new ExcelUtil<AcProjectConfig>(AcProjectConfig.class);
        util.exportExcel(response, list, "工程配置数据");
    }

    /**
     * 获取工程配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('demo:project_config:query')")
    @GetMapping(value = "/{tableId}")
    public AjaxResult getInfo(@PathVariable("tableId") Long tableId) {
        AcProjectConfig acProjectConfig = acProjectConfigService.getById(tableId);
        if (ObjUtil.equal(acProjectConfig.getSysUserId(), getUserId())) {
            return success(acProjectConfig);
        } else {
            return warn("找不到记录");
        }
    }

    /**
     * 新增工程配置
     */
    @PreAuthorize("@ss.hasPermi('demo:project_config:add')")
    @Log(title = "工程配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AcProjectConfig acProjectConfig) {
        return toAjax(acProjectConfigService.save(acProjectConfig));
    }

    /**
     * 修改工程配置
     */
    @PreAuthorize("@ss.hasPermi('demo:project_config:edit')")
    @Log(title = "工程配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AcProjectConfig acProjectConfig) {
        LambdaQueryWrapper<AcProjectConfig> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AcProjectConfig::getSysUserId, getUserId());
        lqw.eq(AcProjectConfig::getTableId, acProjectConfig.getTableId());
        AcProjectConfig tmp = acProjectConfigService.getOne(lqw);
        if (ObjUtil.isNull(tmp)) {
            return warn("未找到可更新数据");
        }
        BeanUtil.copyProperties(acProjectConfig, tmp);
        return toAjax(acProjectConfigService.updateById(acProjectConfig));
    }

    /**
     * 删除工程配置
     */
    @PreAuthorize("@ss.hasPermi('demo:project_config:remove')")
    @Log(title = "工程配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tableIds}")
    public AjaxResult remove(@PathVariable Long[] tableIds) {
        List<Long> delTableIds = new ArrayList<>();
        List<Long> errorTableIds = new ArrayList<>();
        List<AcProjectConfig> list = acProjectConfigService.listByIds(Arrays.asList(tableIds));
        for (AcProjectConfig acProjectConfig : list) {
            if (ObjUtil.equal(acProjectConfig.getSysUserId(), getUserId())) {
                delTableIds.add(acProjectConfig.getTableId());
            } else {
                errorTableIds.add(acProjectConfig.getTableId());
            }
        }
        if (CollUtil.isNotEmpty(errorTableIds)) {
            return warn("未找到可更新数据:" + StrUtil.join(",", errorTableIds));
        } else {
            return toAjax(acProjectConfigService.removeBatchByIds(delTableIds));
        }
    }
}
